package com.newStart;

import com.entity.TestStudent;
import org.apache.ibatis.annotations.Select;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Project: tobe-better-coder
 * Package: com.newStart
 *
 * @author : Mr.xu
 * @date : 2022/8/14 23:36
 */

interface StudentMapper {
    @Select("select * from student")
    List<TestStudent> getList(String name, Integer id);
}

public class TestProxy {
    public static void main(String[] args) {
        StudentMapper mapper = (StudentMapper) Proxy.newProxyInstance(TestProxy.class.getClassLoader(), new Class<?>[]{StudentMapper.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println(Arrays.toString(args));
                Select annotation = method.getAnnotation(Select.class);
                Map<String, Object> nameArgsMap = TestProxy.getNameArgusMap(method, args);
                return null;
            }
        });
        mapper.getList("张三", 1);
    }

    public static Map<String, Object> getNameArgusMap(Method method, Object[] args) throws Exception {
        Map<String, Object> result = new HashMap<>();
        Parameter[] parameters = method.getParameters();
        if (parameters.length != args.length) {
            throw new Exception("参数错误");
        }
        int[] index = {0};
        Arrays.stream(parameters).forEach(parameter -> {
//            parameter.get
//            System.out.println(parameter.getType().getName());
//            System.out.println(parameter.getName());
            result.put(parameter.getName(), args[index[0]]);
            index[0]++;
        });
        return result;
    }

    public static String parseSql(String sql, Map<String, Object> nameArgsMap) {
//        String result = sql.
        for (int i = 0; i < sql.length(); i++) {

        }

        return sql;
    }
}